﻿package com.uprr.app.tnm.test
{
    import com.uprr.app.tnm.command.FooCommand;
    import com.uprr.app.tnm.delegate.ItemFactory;
    import com.uprr.app.tnm.events.CentralDispatcher;
    import com.uprr.app.tnm.events.FooEvent;
    import com.uprr.app.tnm.model.Model;
    
    import mx.events.CollectionEvent;
    import mx.events.CollectionEventKind;
    
    import net.digitalprimates.fluint.tests.TestCase;

    public class FooCommandTest extends TestCase
    {
        public function FooCommandTest()
        {
            super();
        }
        
        private var m_fooCommand : FooCommand;
        
        override protected function setUp():void
        {
            m_fooCommand = new FooCommand();
            m_fooCommand.itemFactory = new ItemFactory();
            
            Model.instance.arrcol.removeAll();
        }
        
        public function testFooCommand() : void
        {
            Model.instance.arrcol.addEventListener(CollectionEvent.COLLECTION_CHANGE,
                asyncHandler( collectionChangeHandler, 3000, null, timeoutHandler));
                
            const fooEvent : FooEvent = new FooEvent();
            fooEvent.col1 = "hello";
            fooEvent.col2 = "world";
            CentralDispatcher.instance.dispatchEvent( fooEvent );            
        }
        
        private function collectionChangeHandler(
            event : CollectionEvent,
            data : Object
        ) : void
        {
            if( event.kind == CollectionEventKind.ADD )
            {
                assertEquals( 1, Model.instance.arrcol.length );
                const obj : Object = Model.instance.arrcol.getItemAt( 0 );
                assertNotNull( obj );
                assertEquals( "hello", obj.col1 );
                assertEquals( "world", obj.col2 );
            }
        }
        
        private function timeoutHandler(data : Object ) : void
        {
            fail( "Did not receive an ADD event" );
        }
        
        override protected function tearDown():void
        {
            Model.instance.arrcol.removeAll();
        }
        
    }
}